﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Interface_Tier;
using Transfer_Object_Tier;
using System.Configuration;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;

namespace eSolution_HRMSS.UserControls
{
    public partial class UC_DanhSachNhanVien : DevExpress.XtraEditors.XtraUserControl
    {
        public UC_DanhSachNhanVien()
        {
            InitializeComponent();
        }
        INhanVien _inhanvien=null;
        ISYS_COMPANYData _ISYS_COMPANYData;
        IHRM_BRANCHData _IHRM_BRANCHData;
        IHRM_DEPARTMENTData _IHRM_DEPARTMENTData;
        IHRM_GROUPData _IHRM_GROUPData;

        DataTable dt = null;
        HRMSS_Ultil util = new HRMSS_Ultil();

        private void UC_DanhSachNhanVien_Load(object sender, EventArgs e)
        {
            
            _inhanvien = (INhanVien)Activator.GetObject(typeof(INhanVien), util.Chuoinv);
            _IHRM_BRANCHData = (IHRM_BRANCHData)Activator.GetObject(typeof(IHRM_BRANCHData), util.Chuoichinhanh);
            _IHRM_DEPARTMENTData = (IHRM_DEPARTMENTData)Activator.GetObject(typeof(IHRM_DEPARTMENTData), util.Chuoiphongban);
            _IHRM_GROUPData = (IHRM_GROUPData)Activator.GetObject(typeof(IHRM_GROUPData), util.ChuoiBT_HRM_GROUPData);
            _ISYS_COMPANYData = (ISYS_COMPANYData)Activator.GetObject(typeof(ISYS_COMPANYData), util.ChuoiSYS_COMPANYData);

            load_ds_nhan_vien_theo_trang_thai();
            load_tree_view();
        }

        private void load_ds_nhan_vien_theo_trang_thai()
        {
            DateTime ngaydautiencuathang=util.FirstDayOfMonthFromDateTime(DateTime.Now);
            DateTime ngaycuoicungcuathang=util.LastDayOfMonthFromDateTime(DateTime.Now);
            
            dt = _inhanvien.LAY_DS_NHAN_VIEN_THEO_TINH_TRANG(1, ngaydautiencuathang, ngaycuoicungcuathang).Tables[0];
            grdDSNhanVien.DataSource = dt;
            col_chinhanh.GroupIndex = 0;
            
        }


        private void barButtonItem9_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            saveFileDialog1.Filter = "Excel File|*.xls";
            saveFileDialog1.Title = "Xuất danh sách ra file MS Excel";
            if (saveFileDialog1.ShowDialog()== DialogResult.OK)
            {
                if (saveFileDialog1.FileName.Length>0)
                {
                    grdDSNhanVien.ExportToXls(saveFileDialog1.FileName.ToString());
                }
            }
            
        }
        
        private void load_tree_view()
        { 
            //load thong tin cong ty lam root
            string CompanyName = _ISYS_COMPANYData.SYS_COMPANY_GetList().Tables[0].Rows[0]["Company"].ToString();
            TreeListNode parent_node = treeList1.AppendNode(null, null);
            parent_node.SetValue("name", CompanyName);
            parent_node.Tag = "";

            foreach (DataRow dr in _IHRM_BRANCHData.HRM_BRANCH_GetList().Tables[0].Rows)
            {
                string brandname = dr["BranchName"].ToString();
                string brandcode = dr["BranchCode"].ToString();

                TreeListNode brand = treeList1.AppendNode(null, parent_node);
                brand.SetValue("name", brandname);
                brand.Tag = brandcode;

                foreach (DataRow dr_dept in _IHRM_DEPARTMENTData.HRM_DEPARTMENT_GetListByBranchCode(brandcode).Tables[0].Rows)
                {
                    string deptname = dr_dept["DepartmentName"].ToString();
                    string deptcode = dr_dept["DepartmentCode"].ToString();

                    TreeListNode dept = treeList1.AppendNode(null, brand);
                    dept.SetValue("name", deptname);
                    dept.Tag = deptcode;

                    foreach (DataRow dr_group in _IHRM_GROUPData.HRM_GROUP_GetListByDepartmentCode(deptcode).Tables[0].Rows)
                    {
                        string GroupName = dr_group["GroupName"].ToString();
                        string GroupCode = dr_group["GroupCode"].ToString();

                        TreeListNode group = treeList1.AppendNode(null, dept);
                        group.SetValue("name", GroupName);
                        group.Tag = GroupCode;

                    }
                }
            }
            treeList1.ExpandAll();
        }

        private void barButtonItem11_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void barButtonItem7_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Winforms.FR_ThongTinNhanVien nv = new Winforms.FR_ThongTinNhanVien(false);
            nv.truyentinhieu = new Winforms.FR_ThongTinNhanVien.updatelist(load_ds_nhan_vien_theo_trang_thai);
            nv.ShowDialog(this);
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            string ma = gridView1.GetFocusedDataRow()[0].ToString();
            eSolution_HRMSS.Winforms.FR_ThongTinNhanVien frnhanvien = new Winforms.FR_ThongTinNhanVien(true, ma);
            frnhanvien.truyentinhieu = new Winforms.FR_ThongTinNhanVien.updatelist(load_ds_nhan_vien_theo_trang_thai);
            frnhanvien.ShowDialog(this);
        }

        private void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string ma = gridView1.GetFocusedDataRow()[0].ToString();
            eSolution_HRMSS.Winforms.FR_ThongTinNhanVien frnhanvien = new Winforms.FR_ThongTinNhanVien(true, ma);
            frnhanvien.truyentinhieu = new Winforms.FR_ThongTinNhanVien.updatelist(load_ds_nhan_vien_theo_trang_thai);
            frnhanvien.ShowDialog(this);
        }

        private void treeList1_DoubleClick(object sender, EventArgs e)
        {
            
        }

        private void treeList1_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            
        }

        private void filter_emp_list(int level, string code)
        {
            DateTime ngaydautiencuathang=util.FirstDayOfMonthFromDateTime(DateTime.Now);
            DateTime ngaycuoicungcuathang=util.LastDayOfMonthFromDateTime(DateTime.Now);
            switch (level)
            {
                case 0:
                    load_ds_nhan_vien_theo_trang_thai();
                    break;
                case 1:
                    grdDSNhanVien.DataSource = _inhanvien.HRM_EMPLOYEE_GetListByBranch(code, 1, ngaydautiencuathang, ngaycuoicungcuathang).Tables[0];
                    col_chinhanh.GroupIndex = 0;
                    col_dept.GroupIndex = -1;
                    col_nhom.GroupIndex = -1;
                    break;
                case 2:
                    grdDSNhanVien.DataSource = _inhanvien.HRM_EMPLOYEE_GetListByDepartment(code, 1, ngaydautiencuathang, ngaycuoicungcuathang).Tables[0];
                    col_chinhanh.GroupIndex = -1;
                    col_dept.GroupIndex = 0;
                    col_nhom.GroupIndex = -1;
                    break;
                case 3:
                    grdDSNhanVien.DataSource = _inhanvien.HRM_EMPLOYEE_GetListByGroup(code, 1, ngaydautiencuathang, ngaycuoicungcuathang).Tables[0];
                    col_chinhanh.GroupIndex = -1;
                    col_dept.GroupIndex = 0;
                    col_nhom.GroupIndex = 0;
                    break;
                default:
                    break;
            }
        }

        private void treeList1_AfterFocusNode(object sender, NodeEventArgs e)
        {
            filter_emp_list(e.Node.Level, e.Node.Tag.ToString());
        }
    }
}
